using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace gozer
{
	internal class Crypt
	{
		public const int MinPassPhraseLength = 8;

		public static string Encrypt(string InputText, string PassPhrase)
		{
			RijndaelManaged RijndaelCipher = new RijndaelManaged();

			byte[] PlainText = Encoding.Unicode.GetBytes(InputText);
			byte[] Salt = Encoding.ASCII.GetBytes(PassPhrase);

			Rfc2898DeriveBytes SecretKey = new Rfc2898DeriveBytes(PassPhrase, Salt);
			ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
			MemoryStream memoryStream = new MemoryStream();
			CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
			cryptoStream.Write(PlainText, 0, PlainText.Length);
			cryptoStream.FlushFinalBlock();
			byte[] CipherBytes = memoryStream.ToArray();
			memoryStream.Close();
			cryptoStream.Close();

			string encryptedData = Convert.ToBase64String(CipherBytes);
			return encryptedData;
		}

		public static string Decrypt(string InputText, string PassPhrase)
		{
			RijndaelManaged RijndaelCipher = new RijndaelManaged();

			byte[] EncryptedData = Convert.FromBase64String(InputText);
			byte[] Salt = Encoding.ASCII.GetBytes(PassPhrase);

			Rfc2898DeriveBytes SecretKey = new Rfc2898DeriveBytes(PassPhrase, Salt);

			ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

			MemoryStream memoryStream = new MemoryStream(EncryptedData);
			CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

			byte[] PlainText = new byte[EncryptedData.Length];

			int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
			memoryStream.Close();
			cryptoStream.Close();

			string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
			return DecryptedData;
		}
	}
}